#pragma once

#include "base.h"

/// A pool of io_context objects.
class io_context_pool : noncopyable {
public:
    /// Construct the io_context pool.
    explicit io_context_pool(std::size_t pool_size);

    /// Run all io_context objects in the pool.
    void run();

    /// Stop all io_context objects in the pool.
    void stop();

    /// Get an io_context to use.
    boost::asio::io_context& get_io_context();

    boost::asio::io_context& get_io_context(size_t index);
private:
    typedef std::shared_ptr<boost::asio::io_context> io_context_ptr;
    typedef std::shared_ptr<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>> work_ptr;

    /// The pool of io_contexts.
    std::vector<io_context_ptr> io_contexts_;

    /// The work that keeps the io_contexts running.
    std::vector<work_ptr> work_;
    std::vector<std::thread> _threads;
    /// The next io_context to use for a connection.
    std::size_t next_io_context_;
};
